module(..., package.seeall)
--- 模块功能：门锁状态反馈
-- @author zhangfenghe
-- @module access_control_system.doorLocStatFeedback
-- @license MIT
-- @copyright openLuat
-- @release 2024.02.27
require "mqtt"
require "misc"
require "nvm"
require "config"
require "mqttHostOutMsg"
require "displayLogic"
require "utilsACS"
-- 初始化配置文件
nvm.init("config.lua", true)
OpenDoorDisplay, OpenDoorSendMsgTime = false, nil
local function openDoorDisplay()
    while true do
        if not OpenDoorDisplay then break end
        displayLogic.OpenDisplay()
        sys.wait(500)
    end
end
-- 获取配置文件信息
local slaveNo, hostNo = nvm.get("ClientNo"), nvm.get("HostNo")
local topic, sendMsg = "", ""
local function gpio24IntFnc(msg)
    log.info("testGpioSingle.gpio24IntFnc", msg, GetGpio24Fnc())
    if msg == cpu.INT_GPIO_NEGEDGE then
        log.info(
            ">>>>>>>>>>>下降沿关门状态触发，门锁反馈逻辑开始！！<<<<<<<<<<<<<<<<<")
        OpenDoorDisplay = false
        nvm.set("OpenDoorDisplay", OpenDoorDisplay)
        if not displayLogic.TimeId then displayLogic.NomalTask() end
        if utilsACS.EmerInterruptFlag then
            if OpenDoorSendMsgTime ~= nil then
                OpenDoorSendMsgTime = sys.timerStop(OpenDoorSendMsgTime)
            end
            displayLogic.OpenDisplayCancel()
        end
        topic = "acs/" .. hostNo
        sendMsg = "{\"action\":\"info\",\"slaveNo\":\"" .. slaveNo ..
                      "\",\"status\":\"0\"}"
        mqttHostOutMsg.InsertHostMsg(topic, sendMsg, 0)
        -- 向主机发送关门状态
    elseif msg == cpu.INT_GPIO_POSEDGE then
        log.info(
            ">>>>>>>>>>>上升沿开门状态触发，门锁反馈逻辑开始！！<<<<<<<<<<<<<<<<<")
        OpenDoorDisplay = true
        nvm.set("OpenDoorDisplay", OpenDoorDisplay)
        if displayLogic.TimeId then displayLogic.NomalTask() end
        if utilsACS.EmerInterruptFlag then
            if OpenDoorSendMsgTime ~= nil then
                OpenDoorSendMsgTime = sys.timerStop(OpenDoorSendMsgTime)
            end
            displayLogic.OpenDisplay()
        else
            if OpenDoorSendMsgTime == nil then
                log.info("开门显示测试2！！！")
                OpenDoorSendMsgTime = sys.timerLoopStart(
                                          displayLogic.OpenDisplay, 1000)
            end
        end

        topic = "acs/" .. hostNo
        sendMsg = "{\"action\":\"info\",\"slaveNo\":\"" .. slaveNo ..
                      "\",\"status\":\"1\"}"
        mqttHostOutMsg.InsertHostMsg(topic, sendMsg, 0)
    end
end

-- 下降沿中断时：msg为cpu.INT_GPIO_POSEDGE；下降沿中断时：msg为cpu.INT_GPIO_NEGEDGE
GetGpio24Fnc = pins.setup(pio.P0_24, gpio24IntFnc)
